#include "linear.h"

int queue_is_empty(struct Queue* queue) {
	if (queue->first == NULL)
		return 1;
	return 0;
}

void enqueue(struct Queue* queue, int item) {
	struct node* oldLast = queue->last;
	queue->last = (struct node *) malloc(sizeof(struct node));
	queue->last->item = item;
	if (queue_is_empty(queue)) {
		queue->first = queue->last;
		queue->n = 1;
	} else {
		oldLast->next = queue->last;
		(queue->n)++;
	}
}

int dequeue(struct Queue* queue) {
	struct node* node = queue->first;
	queue->first = node->next;
	if (queue_is_empty(queue)) {
		queue->last = NULL;
	}
	(queue->n)--;
	int res = node->item;
	free((void *) node);
	return res;
}

void main1() {
	struct Queue* queue;

	queue = (struct Queue *) malloc(sizeof(struct Queue));
	queue->first = NULL;
	queue->last = NULL;
	enqueue(queue, 1);
	enqueue(queue, 2);
	enqueue(queue, 3);

	while (!queue_is_empty(queue))
		printf("size is %d, res is %d\n", queue->n, dequeue(queue));
}
